<script type="text/javascript">
<!--
	$(function(){
		$('#addUsers').validate({
			rules:{
				userName:{
					required: true,
					maxlength: 20
				},
				userPassword:{
					required: true,
					minlength: 6
				},
				rePassword:{
					required: true,
					equalTo: '#userPassword'
				},
				userFullName:{
					required: true
				},
				userTypeId:{
					required: true,
					min: 1
				}
		},
		messages:{
				userName:{
					required: 'Plese enter a user name',
					maxlength: 'User name should be less than 20 characters'
				},
				userPassword: {
					required: 'Please enter a password',
					minlength: 'Password should be minimum of 6 characters'
				},
				rePassword: {
					required: 'Please enter a password',
					equalTo: "Password and Re-Password should be equal"
				},
				userFullName: '<br/>Please enter the full name of the user',
				userTypeId:{
					required: 'Please select the user type',
					min: 'Please select the user type'
				}                       
			}
		});
	});
	function isNumberKey(evt){
        var charCode = (evt.which) ? evt.which : event.keyCode
        if (charCode > 31 && (charCode < 48 || charCode > 57))
            return false;
        return true;
    }
//-->
</script>
<?php include './pages/adminMenu.php'; 
	if(isset($_SESSION['userTypeId'])){
		$typeId = $_SESSION['userTypeId']; 
	}?>
	
<div class="mainContent">
	<div class="masterData">
		<h3>Current Users</h3>
		<?php
			include('./system/dbConnection.php');
			if($result=mysql_query("SELECT * FROM users, user_types WHERE users.userTypeId = user_types.typeId")){
				if(mysql_num_rows($result)){			
					echo "<table border = '1px' cellspacing='0' width='100%' class='masterTable'>";
					echo "<tr><th>User</th><th>Designation</th><th>Status</th></tr>";
					while($row = mysql_fetch_array($result)){
						echo "<tr>";
						echo "<td>" . $row['userName'] . "</td>";
						echo "<td>" . $row['typeName'] . "</td>";
						echo "<td>" . $row['status'] . "</td>";
						echo "</tr>";
					}
					echo "</table>";
				}else{
					echo "No Data in Database";
				}	
			}
		?>
	</div>
	<div class="mainForm">
		<form action="./system/action/addUsers.php" name="addUsers" id="addUsers" method="POST">	
			<table id="addUserForm" class="formTable" width="100%">
				<col width="150px"/>
				<col/>
				<tr>
					<td colspan="2">
						<?php
						if(isset($_SESSION['error']) && $_SESSION['error'] != ""){ ?>
							<div class="error">
								<?php echo $_SESSION['error']; ?>
							</div>
						<?php } else if(isset($_SESSION['success']) && $_SESSION['success'] != ""){?>
							<div class="success">
								<?php echo $_SESSION['success']; ?>
							</div>
						<?php }
						?>
					</td>
				</tr>
				<tr>
					<td>User Name <label class="mandatory">*</label></td>
					<td><input type="text" name="userName" id="userName" class="width_200"/></td>
				</tr>				
				<tr>
					<td>Full Name <label class="mandatory">*</label></td>
					<td><textarea name="userFullName" id="userFullName" class="width_200"></textarea></td>
				</tr>
				<tr>
					<td>User Type <label class="mandatory">*</label></td>
					<td>
						<select name="userTypeId" id="userTypeId" class="width_200">
							<option value="0">Select</option>
							<?php 
								include './system/dbconnection.php';
								if($res = mysql_query("SELECT * FROM user_types WHERE typeId > $typeId")){
									while($row = mysql_fetch_assoc($res)){
										echo "<option value='".$row['typeId']."'>".$row['typeName']."</option>";
									}
								}
							?>
						</select>
					</td>
				</tr>
				<tr>
					<td>Address</td>
					<td><textarea name="userAddress" id="userAddress" class="width_200"></textarea></td>
				</tr>
				<tr>
					<td>Telephone</td>
					<td><input type="text" name="userTelephone" id="userTelephone" class="width_200" onkeypress="return isNumberKey(event)"/></td>
				</tr>
				<tr>
					<td></td>
					<td><input type="submit" value="Add User"/></td>
				</tr>
			</table>
		</form>
	</div>
</div>